﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClassLibrary.DAO;
using ClassLibrary.Entity;

namespace mprsc
{
    public partial class NewProduct : Form
    {
        TblProductsDAO ProductsDAO = new TblProductsDAO(Global.CONNECTION_STRING);
        TblPhoneBranDAO phoneBrandDAO = new TblPhoneBranDAO(Global.CONNECTION_STRING);
        TblModelDAO modelDAO = new TblModelDAO(Global.CONNECTION_STRING);
        TblJobsDAO JobsDAO = new TblJobsDAO(Global.CONNECTION_STRING);

        public delegate void SendMessage(string Message);
        public SendMessage Sender; 

        public NewProduct()
        {
            InitializeComponent();

            //Tao con tro toi hàm GetMessage
            Sender = new SendMessage(GetMessage);

            LoadTypeProduct();
            LoadBrandPhone();
        }

        //Hàm có nhiem vu lay tham so truyen vào
        private void GetMessage(string Message)
        {
            txtProductID.Text = Message;
        }

        // load product Sell - part
        private void LoadTypeProduct()
        {
            var Type = new Dictionary<int, string>();
            Type.Add(1, "Part");
            Type.Add(2, "Sell");
            cbPartorSell.DataSource = new BindingSource(Type, null);
            cbPartorSell.DisplayMember = "Value";
            cbPartorSell.ValueMember = "Key";
        }
        private void LoadModel()
        {
            cbModel.DataSource = modelDAO.GetModel();
            cbModel.DisplayMember = "Model";
            cbModel.ValueMember = "IDModel";

        }
        private void LoadBrandPhone()
        {
            cb_menuFac.DataSource = JobsDAO.LoadBrandPhone();
            cb_menuFac.DisplayMember = "BrandName";
            cb_menuFac.ValueMember = "IDBrand";

        }
        private void NewProduct_Load(object sender, EventArgs e)
        {
            this.CenterToParent();
            if (txtProductID.Text != "")
            {
                loadDetailProduct();
            }
            LoadBrandPhone();
        }

        private void loadModel()
        {
            if (cb_menuFac.SelectedValue == null)
            {
                cbModel.DataSource = modelDAO.GetModel();
                cbModel.DisplayMember = "Model";
                cbModel.ValueMember = "IDModel";
            }
            else
            {
                DataTable dt = modelDAO.GetModelByIdBrand(Convert.ToInt32(cb_menuFac.SelectedValue.ToString()));
                foreach (DataRow dr in dt.Rows)
                {
                    cbModel.DataSource = dt;
                    cbModel.DisplayMember = "Model";
                    cbModel.ValueMember = "IDModel";
                }
            }
        }

        private void cb_menuFac_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_menuFac.SelectedValue.ToString() == "System.Data.DataRowView")
            {
                cbModel.DataSource = modelDAO.GetModel();
                cbModel.DisplayMember = "Model";
                cbModel.ValueMember = "IDModel";
            }
            else
            {
                DataTable dt = modelDAO.GetModelByIdBrand(Convert.ToInt32(cb_menuFac.SelectedValue.ToString()));
                foreach (DataRow dr in dt.Rows)
                {
                    cbModel.DataSource = dt;
                    cbModel.DisplayMember = "Model";
                    cbModel.ValueMember = "IDModel";
                }        
            }
        }

        // load product detail
        private void loadDetailProduct()
        {
            string idProd = txtProductID.Text;
            DataTable productDT = ProductsDAO.GetProductById(Convert.ToInt32(idProd));
            foreach (DataRow dr in productDT.Rows)
            {
                txtProductID.Text = dr["ProductID"].ToString();
                txtProductName.Text = dr["ProductName"].ToString();
                txtProductCode.Text = dr["productCode"].ToString();
                cbModel.SelectedValue = dr["IDmodel"].ToString();
                cb_menuFac.SelectedValue = dr["IDPhoneBrand"].ToString();
                txtType.Text = dr["ProductType"].ToString();
                txtDescriptions.Text = dr["ProductDescription"].ToString();
                txtCost.Text = dr["Cost"].ToString();
                txtRRP.Text = dr["RRP"].ToString();
                txtQuantity.Text = dr["QuantityOnHand"].ToString();
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtCost.Text == "" || txtQuantity.Text == "" || txtRRP.Text == "" || txtProductName.Text == "" || txtProductCode.Text == "")
            {
                this.validationProvider1.ValidationMessages(!this.validationProvider1.Validate());
            }
            else
            {
                bool flag = false;
                try
                {
                    TblProducts product = new TblProducts();
                    product.ProductName = txtProductName.Text;
                    product.ProductCode = txtProductCode.Text;
                    product.IDPhoneBrand = Convert.ToInt32(cb_menuFac.SelectedValue);
                    product.IDModel = Convert.ToInt32(cbModel.SelectedValue);
                    product.ProductType = txtType.Text;
                    product.ProductDescription = txtDescriptions.Text;
                    product.Cost = Convert.ToDecimal(txtCost.Text);
                    product.RRP = Convert.ToDecimal(txtRRP.Text);
                    product.QuantityOnHand = Convert.ToInt32(txtQuantity.Text);
                    product.Type = Convert.ToInt32((cbPartorSell.SelectedValue));

                    if (txtProductID.Text == "")
                    {
                        product.ProductID = 0;
                        ProductsDAO.InsUpdProducts(product, 1);
                    }
                    else
                    {
                        product.ProductID = Convert.ToInt32(txtProductID.Text);
                        ProductsDAO.InsUpdProducts(product, 2);
                    }

                    flag = true;

                }
                catch (Exception ex)
                {
                    throw ex;
                }
                if (flag)
                {
                    MessageBox.Show("Insert Product Sucessful!");
                    
                    ListProduct product = new ListProduct();
                    product.Show();
                    this.Hide();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtDescriptions.Text = txtCost.Text = txtProductCode.Text = txtProductName.Text = string.Empty;
            txtQuantity.Text = txtRRP.Text = txtType.Text = string.Empty;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
